<?php
/**
 * Created by PhpStorm.
 * User: koterwong
 * Date: 2017/8/29
 * Time: 17:21
 */

namespace app\api\model;

use think\Db;

class UserModel extends BaseModel
{
    public function checkToken($user_token)
    {
        $result = Db::name('user_token')->where(['token' => $user_token])->find();
        if ($result) {
            if ($result['expire_time'] < time()) {
                returnError(API_AUTH_TIME_OUT);
            }

            if (!defined('API_UID')) {
                define('API_UID', $result['user_id']);
            }
        } else {
            returnError(500, '登录信息为空');
        }
    }

    //复制user模块
    public function registerUser($user)
    {
        $result = Db::name("user")->where('mobile', $user['mobile'])->find();

        $userStatus = 1;

        if (cmf_is_open_registration()) {
            $userStatus = 2;
        }

        if (empty($result)) {
            $data = [
                'user_login' => $user['mobile'],
                'user_email' => '',
                'mobile' => $user['mobile'],
                'user_nickname' => '',
                'user_pass' => cmf_password($user['user_pass']),
                'last_login_ip' => get_client_ip(0, true),
                'create_time' => time(),
                'last_login_time' => time(),
                'user_status' => $userStatus,
                "user_type" => 2,//会员
            ];
            $userId = Db::name("user")->insertGetId($data);
            $data = Db::name("user")->where('id', $userId)->find();
            cmf_update_current_user($data);
            return 0;
        }
        return 1;
    }

    /**
     * @param $user
     * @return int  3用户被禁用、 2用户不存在  1密码不正确  成功返回user
     */
    public function mobileLogin($user)
    {
        $userQuery = Db::name("user");

        $result = $userQuery->where('user_login', $user['mobile'])->find();

        if (!empty($result)) {
            $comparePasswordResult = cmf_compare_password($user['user_pass'], $result['user_pass']);
            $hookParam = [
                'user' => $user,
                'compare_password_result' => $comparePasswordResult
            ];
            hook_one("user_login_start", $hookParam);
            if ($comparePasswordResult) {
                //拉黑判断。
                if ($result['user_status'] == 0) {
                    return 3;
                }
                session('user', $result);
                $data = [
                    'last_login_time' => time(),
                    'last_login_ip' => get_client_ip(0, true),
                ];
                $userQuery->where('id', $result["id"])->update($data);
                return $result;
            }
            return 1;
        }
        $hookParam = [
            'user' => $user,
            'compare_password_result' => false
        ];
        hook_one("user_login_start", $hookParam);
        return 2;
    }

    /**
     * @param $user
     * @return int   1 用户不存在  2 与原密码相同  0修改密码成功
     */
    public function resetPwd($user)
    {
        $userQuery = Db::name("user");

        $result = $userQuery->where('mobile', $user['mobile'])->find();
        if (!empty($result)) {
//            $comparePasswordResult = cmf_compare_password($user['user_pass'], $result['user_pass']);
//            if (!$comparePasswordResult) {
//                //更新用户密码
            $data = [
                'user_pass' => cmf_password($user['user_pass']),
            ];
            $result = $userQuery->where('mobile', $user['mobile'])->update($data);
            if ($result) {
                return 0;
            }
//            } else {
//                return 2;
//            }
        }
        return 1;
    }

    public function editNick($user)
    {
        $user_id = $user['user_id'];
        unset($user['user_id']);
        $userQuery = Db::name("user");
        $result = $userQuery->where('id', $user_id)->update($user);
        if ($result) {
            return 1;
        } else {
            return -1;
        }
    }

}